package com.zengqingfa.demo.service.impl;

import com.zengqingfa.demo.annotation.AspectAnnotation;
import com.zengqingfa.demo.entity.SysDept;
import com.zengqingfa.demo.entity.SysUser;
import com.zengqingfa.demo.mapper.SysDeptMapper;
import com.zengqingfa.demo.mapper.SysUserMapper;
import com.zengqingfa.demo.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.List;

/**
 * @author zengqingfa
 * @date 2019/10/14 14:23
 * @description
 * @email zengqingfa_java@163.com
 */
@Service
public class UserServiceImpl implements UserService {

    private static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;


    @Override
    @AspectAnnotation(isStart = true)
    public SysUser getById(Long id) {
        return sysUserMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SysUser> findAll() {
        return sysUserMapper.findAll();
    }

//测试 spring事务
//事务生效
/*    @Override
    @Transactional
    public void testTransaction1() {
        SysDept sysDept = new SysDept();
        sysDept.setDeptName("部门1");
        sysDeptMapper.insert(sysDept);
        int i = 1 / 0;
        SysUser sysUser = new SysUser();
        sysUser.setUserName("user11111");
        sysUserMapper.insert(sysUser);
    }*/


//指定回滚异常
  /*  @Override
    @Transactional(rollbackFor = Exception.class)
    public void testTransaction1() throws SQLTimeoutException {
        SysDept sysDept = new SysDept();
        sysDept.setDeptName("部门1");
        sysDeptMapper.insert(sysDept);
        SysUser sysUser = new SysUser();
        sysUser.setUserName("user11111");
        sysUserMapper.insert(sysUser);
        throw new SQLTimeoutException();
    }*/

    //处理之后不生效
   /* @Override
    @Transactional
    public void testTransaction1() {
        try {
            SysDept sysDept = new SysDept();
            sysDept.setDeptName("部门1");
            sysDeptMapper.insert(sysDept);
            int i = 1 / 0;
            SysUser sysUser = new SysUser();
            sysUser.setUserName("user11111");
            sysUserMapper.insert(sysUser);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/

    @Override
    @Transactional
    public void testTransaction1() {
        try {
            SysDept sysDept = new SysDept();
            sysDept.setDeptName("部门1");
            sysDeptMapper.insert(sysDept);
            int i = 1 / 0;
            SysUser sysUser = new SysUser();
            sysUser.setUserName("user11111");
            sysUserMapper.insert(sysUser);
        }catch (Exception e){

        }

    }


  /*  @Transactional
    public void testTransaction3() {
        SysDept sysDept = new SysDept();
        sysDept.setDeptName("部门1");
        sysDeptMapper.insert(sysDept);
        int i = 1 / 0;
        SysUser sysUser = new SysUser();
        sysUser.setUserName("user11111");
        sysUserMapper.insert(sysUser);
    }*/

    @Autowired
    TransactionTemplate transactionTemplate;


    @Override
    public void testTransaction2() {
        //使用编程式事务
        boolean result = transactionTemplate.execute(new TransactionCallback<Boolean>() {
            @Override
            public Boolean doInTransaction(TransactionStatus status) {
                try {
                    SysDept sysDept = new SysDept();
                    sysDept.setDeptName("部门1");
                    sysDeptMapper.insert(sysDept);
                    int i = 1 / 0;
                    SysUser sysUser = new SysUser();
                    sysUser.setUserName("user11111");
                    sysUserMapper.insert(sysUser);
                } catch (Exception e) {
                    status.setRollbackOnly();
                    logger.error(e.getMessage(), e);
                    return false;
                }
                return true;
            }
        });
    }

    public void testTransaction22() {
        //使用编程式事务  Lambda表达式版本
        transactionTemplate.execute((status) -> {
            try {
                SysDept sysDept = new SysDept();
                sysDept.setDeptName("部门1");
                sysDeptMapper.insert(sysDept);
                int i = 1 / 0;
                SysUser sysUser = new SysUser();
                sysUser.setUserName("user11111");
                sysUserMapper.insert(sysUser);

            } catch (Exception e) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); //手动开启事务回滚
                logger.error(e.getMessage(), e);
                return false;
            }
            return true;
        });
    }


}
